package com.extension.company.view.appcome;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.extension.company.admin.R;
import com.extension.company.view.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
public class LeadActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

	private ViewPager viewPager;
	private Button experienceBtn, jumpLeader;
	private MyViewPagerAdapter viewPagerAdapter;
	private int[] imgView;
	private List<View> list;
	private LinearLayout ll;
	private List<ImageView> hotList;
	private int prePosition;

	public static void Intent(Activity activity) {
		Intent intent = new Intent(activity, LeadActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leader);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		imgView = new int[]{
				R.drawable.people_header, R.drawable.index_red, R.drawable.item_goods_ensure
		};
		list = new ArrayList<>();
		hotList = new ArrayList<>();
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

		viewPager = findViewById(R.id.leader_viewpager);
		experienceBtn = findViewById(R.id.experience);
		findViewById(R.id.jump_leader);
		ll = findViewById(R.id.viewpager_indotor);
		jumpLeader = findViewById(R.id.jump_leader);

		viewPager.addOnPageChangeListener(this);
		jumpLeader.setOnClickListener(this);
		experienceBtn.setOnClickListener(this);

		for (int i = 0; i < imgView.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(params);
			imageView.setImageResource(imgView[i]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			list.add(imageView);
		}

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		layoutParams.rightMargin = 20;

		for (int i = 0; i < imgView.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(layoutParams);
			imageView.setImageResource(R.drawable.leader_gray_point);
			ll.addView(imageView);
			hotList.add(imageView);
		}

		hotList.get(0).setImageResource(R.drawable.leader_red_point);

		viewPagerAdapter = new MyViewPagerAdapter(list);
		viewPager.setAdapter(viewPagerAdapter);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(LeadActivity.this, MainActivity.class);
		switch (v.getId()) {
			case R.id.experience:
				startActivity(intent);
				finish();
				break;
			case R.id.jump_leader:
				startActivity(intent);
				finish();
				break;
			default:
				break;
		}
	}

	@Override
	public void onPageScrolled(int i, float v, int i1) {

	}

	@Override
	public void onPageSelected(int i) {
		Log.d("zwk", i + "位置");
		if (i == list.size() - 1) {
			experienceBtn.setVisibility(View.VISIBLE);
		} else {
			experienceBtn.setVisibility(View.INVISIBLE);
		}
		hotList.get(prePosition).setImageResource(R.drawable.leader_gray_point);
		hotList.get(i).setImageResource(R.drawable.leader_red_point);
		prePosition = i;

	}

	@Override
	public void onPageScrollStateChanged(int i) {

	}


	class MyViewPagerAdapter extends PagerAdapter {

		private List<View> listImg;

		MyViewPagerAdapter(List<View> viewList) {
			this.listImg = viewList;
		}

		@Override
		public int getCount() {
			if (listImg == null) {
				return 0;
			}
			return listImg.size();
		}

		@Override
		public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
			return view == o;
		}

		@NonNull
		@Override
		public Object instantiateItem(@NonNull ViewGroup container, int position) {
			container.addView(listImg.get(position));
			return listImg.get(position);
		}

		@Override
		public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
			container.removeView((View) object);
		}
	}
}
